﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("~/Login.aspx");
            return;
        }
        if (Session["ProIdCard"] == null)
        {
            Response.Redirect("~/Default.asspx");
            return;
        }
        ShoppingDataContext spdt = new ShoppingDataContext();
        var mem = from p in spdt.MEMBERs
                  where p.username == Session["user"].ToString()
                  select p;
        this.txtName.Text = mem.First().Fullname;
        this.txtEmail.Text = mem.First().email;
        this.txtTelephone.Text = mem.First().phone;



    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ShoppingDataContext spdt = new ShoppingDataContext();
        var bookeds = from p in spdt.PRO_BOOKEDs
                     select p;
        int temp = 1;
        int flag1 = 0;
        int flag = 1;
        int ProId = 0;
        string tam = "B";
        while (flag != 0)
        {
            foreach (var booked in bookeds)
            {
                string id = booked.BookedID.Substring(1, 4);
                ProId = int.Parse(id);
                if (ProId == temp)
                {
                    temp++;
                    flag1 = 0;
                    break;
                }

                flag1 = 1;
            }
            if (flag1 == 1)
            {
                flag = 0;
                ProId = temp;
            }

        }
        for (int i = 1; i <= 4 - ProId.ToString().Length; i++)
        {
            tam = tam + "0";
        }
        tam = tam + ProId.ToString();

        var members = from p in spdt.MEMBERs
                      where p.username == Session["user"].ToString()
                      select p;

        PRO_BOOKED pro_booked = new PRO_BOOKED();
        pro_booked.BookedID = tam;
        pro_booked.MemberID = members.First().MemberID;
        pro_booked.Fullname = Hotennguoinhan.Text;
        pro_booked.phone = sodienthoainguoinhan.Text;
        pro_booked.address = diachigiaohang.Text;
        pro_booked.bookeddate = DateTime.Today;
        pro_booked.deliverydate = DateTime.Today.AddDays(7);
        pro_booked.finish = false;
        spdt.PRO_BOOKEDs.InsertOnSubmit(pro_booked);

        ArrayList ProIdCard = (ArrayList)Session["ProIdCard"];
        ArrayList CountCard = (ArrayList) Session["CountCard"];
        for (int i = 0; i < ProIdCard.Count; i++)
        {
            PRO_BOOKED_DE pro_booked_de = new PRO_BOOKED_DE();
            pro_booked_de.BookedID = tam;
            pro_booked_de.Product_DesID = ProIdCard[i].ToString();
            pro_booked_de.amount = int.Parse(CountCard[i].ToString());
            spdt.PRO_BOOKED_DEs.InsertOnSubmit(pro_booked_de);

            var prodes = spdt.PRODUCT_DEs.Single(p => p.Product_DesID == ProIdCard[i].ToString());
            if (prodes.number - int.Parse(CountCard[i].ToString()) >= 0)
            {
                prodes.number -= int.Parse(CountCard[i].ToString());
            }
            else
            {
            }

        }
        spdt.SubmitChanges();
    }
}
